// (Sum the digits in an integer) Write a program that reads an integer between 0 and 
// 1000 and adds all the digits in the integer. For example, if an integer is 932, the 
// sum of all its digits is 14.
// Hint: Use the % operator to extract digits, and use the / operator to remove the 
// extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.

import java.util.Scanner;

public class test26 {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            // 提示用户输入0-1000之间的整数
            System.out.print("Enter an integer between 0 and 1000: ");
            int number = input.nextInt();
            
            // 验证输入范围
            if (number < 0 || number > 1000) {
                System.out.println("Invalid input! Number must be between 0 and 1000.");
                return;
            }
            
            // 计算各位数字之和
            int sum = 0;
            int temp = number;
            
            while (temp > 0) {
                int digit = temp % 10;  // 获取最后一位数字
                sum += digit;           // 累加到总和
                temp /= 10;             // 移除最后一位数字
            }
            
            // 输出结果
            System.out.println("The sum of all digits is " + sum);
        }
    }
}